package com.fizzkai.p2p.bussness.service.impl;

import com.fizzkai.p2p.bussness.domain.RechargeOffline;
import com.fizzkai.p2p.bussness.mapper.RechargeOfflineMapper;
import com.fizzkai.p2p.bussness.service.IAccountFlowService;
import com.fizzkai.p2p.bussness.service.IRechargeOfflineService;
import com.fizzkai.p2p.core.domain.Account;
import com.fizzkai.p2p.core.exception.DisplayableException;
import com.fizzkai.p2p.core.query.QueryObject;
import com.fizzkai.p2p.core.service.IAccountService;
import com.fizzkai.p2p.core.util.AssertUtil;
import com.fizzkai.p2p.core.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service@Transactional
public class RechargeOfflineServiceImpl implements IRechargeOfflineService {

    @Autowired
    private RechargeOfflineMapper rechargeOfflineMapper;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IAccountFlowService accountFlowService;

    @Override
    public int save(RechargeOffline entity) {
        return rechargeOfflineMapper.insert(entity);
    }

    @Override
    public int update(RechargeOffline entity) {
        return rechargeOfflineMapper.updateByPrimaryKey(entity);
    }

    @Override
    public RechargeOffline get(Long id) {
        return rechargeOfflineMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<RechargeOffline> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<RechargeOffline> list = rechargeOfflineMapper.queryList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void apply(RechargeOffline entity) {
        RechargeOffline offline = new RechargeOffline();

        // 前台 传过来的 参数
        offline.setBankInfo(entity.getBankInfo());
        offline.setTradeCode(entity.getTradeCode()); // 交易号
        offline.setRechargeDate(entity.getRechargeDate());  // 充值日期
        offline.setAmount(entity.getAmount());
        offline.setNote(entity.getNote());
        // 后台设置 参数
        offline.setApplier(UserContext.getCurrentLoginInfo()); // 申请人
        offline.setApplyTime(new Date()); // 申请日期
        offline.setState(RechargeOffline.STATE_NORMAL); //待审核 状态
        this.save(offline);
    }

    @Override
    public void audit(Long id, Integer state, String remark) {
        // 获取 原来的 RechargeOffline
        RechargeOffline offline = rechargeOfflineMapper.selectByPrimaryKey(id);
        // 杜绝非法参数
        if (offline == null // 不存在
            || offline.getState() != RechargeOffline.STATE_NORMAL   // 状态不是 待审核
            ) {
            throw new DisplayableException("非法操作");
        }
        // 审核
        if (state == RechargeOffline.STATE_PASS) {
            // 审核 通过
            offline.setState(RechargeOffline.STATE_PASS);
            // 给账户充值
            Account account = accountService.get(offline.getApplier().getId());
            account.setUsableAmount(account.getUsableAmount().add(offline.getAmount()));
            accountService.update(account);
            // 保存流水
            accountFlowService.createRechargeOfflineFlow(account, offline.getAmount(), remark);
        } else {
            // 审核 不通过
            offline.setState(RechargeOffline.STATE_REJECT);
        }
        // 审核备注
        offline.setRemark(remark);  // 注意 note 是申请人 写的 备注
        // 设置 后台 参数
        offline.setAuditor(UserContext.getCurrentLoginInfo());
        offline.setAuditTime(new Date());
        this.update(offline);

    }
}
